package ai.yiye.leveldbdemo;

import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBIterator;
import org.iq80.leveldb.Options;
import org.iq80.leveldb.WriteBatch;

import java.io.File;

import static org.fusesource.leveldbjni.JniDBFactory.*;

public class Main {

    public static void main(String[] args) throws Exception {
        Options options = new Options();
        options.createIfMissing(true);
        //
        try(DB db = factory.open(new File("/home/wukong/exmaple"), options)) {
            db.put(bytes("Tampa"), bytes("时间你好"));
            String value = asString(db.get(bytes("Tampa")));
            System.out.println(value);

            WriteBatch batch = db.createWriteBatch();
            for(int i =0; i< 10; i++){
                batch.put(bytes("test"+ i), bytes("test value" + i));
            }
            db.write(batch);
            //

            DBIterator iterator = db.iterator();
            for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()){
                String key = asString(iterator.peekNext().getKey());
                String val = asString(iterator.peekNext().getValue());
                //
                System.out.println(key + "=>" + val);
            }
        }



    }
}
